package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.dtos.WmSensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 敏感词信息表 服务实现类
 * </p>
 *
 * @author 一叠温酒
 */
@Slf4j
@Service
@Transactional
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {

    @Autowired
    private WmSensitiveMapper wmSensitiveMapper;

    /**
     * 分页查询,模糊查询
     * @param sensitiveDto
     * @return
     */
    @Override
    public PageResponseResult list(SensitiveDto sensitiveDto) {
        String name = sensitiveDto.getName();
        Integer page = sensitiveDto.getPage();
        Integer size = sensitiveDto.getSize();
      return   name ==null ||"".equals(name) ? listPage(page,size) : dimlist(name,page,size);

    }

    /**
     * 模糊查询
     * @param name
     * @param page
     * @param size
     * @return
     */
    private PageResponseResult dimlist(String name, Integer page, Integer size) {
        Page<WmSensitive> wmSensitivePage = new Page<>(page ,size);
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(WmSensitive::getCreatedTime);
        wrapper.like(WmSensitive::getSensitives,"%"+name+"%");
         this.page(wmSensitivePage,wrapper);
        PageResponseResult pageResponseResult = new PageResponseResult();
        pageResponseResult.setData(wmSensitivePage.getRecords());
        pageResponseResult.setTotal((int) wmSensitivePage.getTotal());
        pageResponseResult.setCurrentPage(page);
        return pageResponseResult;
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    private PageResponseResult listPage(Integer page, Integer size) {
        Page<WmSensitive> wmSensitivePage = new Page<>(page,size);
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(WmSensitive::getCreatedTime);
        this.page(wmSensitivePage,wrapper);
        PageResponseResult pageResponseResult = new PageResponseResult();
        pageResponseResult.setData(wmSensitivePage.getRecords());
        pageResponseResult.setTotal((int) wmSensitivePage.getTotal());
        pageResponseResult.setCurrentPage(page);
        return pageResponseResult;
    }
}
